package com.yunbao.hujin.mymvpdemo1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yunbao.hujin.mymvpdemo1.define.ViewFind;
import com.yunbao.hujin.mymvpdemo1.request.WeatherBean;
import com.yunbao.hujin.mymvpdemo1.define.FieldView;
import com.yunbao.hujin.mymvpdemo1.mvp.RequestPresenter1;
import com.yunbao.hujin.mymvpdemo1.mvp.RequestView1;

/**
 * 最简单的mvp模式,
 * 1.Activity需要实现v层接口
 * 2.Persenter需要持有v层引用和m层引用
 * 3.在实现类view中创建persenter
 *
 * 可是这样写会内存泄露，因为如果在网络请求的过程中Activity就关闭了，
 * Presenter还持有了V层的引用，也就是MainActivity，就会内存泄露。
 */
public class MainActivity extends AppCompatActivity implements RequestView1 {

    @FieldView(R.id.tv_text)
    private TextView textView;

    private RequestPresenter1 presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewFind.bind(this);
        //创建Presenter
        presenter = new RequestPresenter1(this);
    }

    //Button点击事件
    public void request(View view) {
        presenter.clickRequest("101210101");    //杭州天气
    }


    /********----View层RequestView1接口----************/
    //请求时加载
    @Override
    public void requestLoading() {
        textView.setText("请求中,请稍后...");
    }

    //请求成功
    @Override
    public void resultSuccess(WeatherBean bean) {
        textView.setText(bean.getWeatherinfo().toString());
    }

    //请求失败
    @Override
    public void resultFailure(String result) {
        textView.setText(result);
    }
}
